package com.kzw.oa.exam.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.kzw.core.bean.Msg;
import com.kzw.misc.utils.ContextUtil;
import com.kzw.oa.exam.model.ExamResult;
import com.kzw.oa.exam.model.Examine;
import com.kzw.oa.exam.service.ExamResultService;
import com.kzw.oa.exam.service.ExamineService;
import com.kzw.oa.exam.service.QuestionService;
import com.kzw.oa.system.model.AppUser;

@Controller("studentAction2")
@RequestMapping("/exam/student")
@SessionAttributes({"currUser"})
public class StudentAction {

	@Autowired
	private QuestionService questionService;
	@Autowired
	private ExamResultService examResultService;
	@Autowired
	private ExamineService examService;
	

	/**
	 * 根据该学生的班级，获得可用的考试
	 * */
	@RequestMapping("index")
	public String index(Model model) {
		AppUser currUser = ContextUtil.getCurrentUser();
		model.addAttribute("currUser", currUser);
		
		// 当前用户的考试列表：未开始和进行中
		String hql = "from Examine where status!=2 and dept.depId=? order by status desc";
		List<Examine> list = examService.findByHQL(hql, currUser.getDept().getOrgSupId());
		model.addAttribute("list", list);
		
		return "student_index";
	}

	/**
	 * 进入考试
	 * */
	@RequestMapping("exam")
	public String exam(Model model, Long examId) {
		// 开始考试（进行中）
		Examine examine = examService.get(examId);
		if(examine.getStatus() != 1) {
			if(examine.getStatus() == 0) {
				model.addAttribute("msg", "考试未开始!");
			} else if(examine.getStatus() == 2) {
				model.addAttribute("msg", "考试已结束!");
			}
			return "student_index";
		}		
		
		// 数据库中查询，如果没有题目信息则生成，如果有题目信息则读取
		List<Map<String, Object>> data = questionService.getMyQuestion(examId);
		model.addAttribute("data", data);
		model.addAttribute("student", ContextUtil.getCurrentUser());
		model.addAttribute("examine", examine);
		
		return "student_exam";
	}
	
	/**
	 * 保存答案， FIXME
	 * */
	@RequestMapping("saveExam")
	public String saveExam() {
		AppUser student = ContextUtil.getCurrentUser();
		String hql = "from ExamResult where examine.oid=? and student=?";
		ExamResult examRet = questionService.findUniqueByHQL(hql, 1L, student);
		
		// 保存答案
		examRet.setStatus(1);
		examRet.setCtime(new Date());
		
		examResultService.saveOrUpdate(examRet);
		
		return "redirect:/exam/student/index";
	}
	
	/**
	 * 查看学生试卷，并打分（每一个题目）
	 * */
	@RequestMapping("scoreExam/{oid}")
	public String scoreExam(@PathVariable Long oid, Model model) {
		ExamResult examRet = examResultService.get(oid);
		
		List<Map<String, Object>> data = questionService.getQuestion(examRet);
		model.addAttribute("data", data);
		model.addAttribute("student", examRet.getStudent());
		model.addAttribute("examine", examRet.getExamine());
		
		// 数据库中查询，如果没有题目信息则生成，如果有题目信息则读取
		return "student_exam2";
	}
	
	/**
	 * 评分（细分项）
	 * */
	@ResponseBody
	@RequestMapping("saveScore")
	public Msg saveScore(Long oid, Integer score) {
		ExamResult examRet = examResultService.get(oid);
		examRet.setScore(score);
		examRet.setStatus(2);
		examResultService.saveOrUpdate(examRet);
		
		return new Msg();
	}
}
